package com.zero.logistics.controller;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.logistics.pojo.MessageObject;
import com.zero.logistics.pojo.BaseData;
import com.zero.logistics.pojo.BaseDataExample;
import com.zero.logistics.pojo.BaseDataExample.Criteria;
import com.zero.logistics.pojo.Customer;
import com.zero.logistics.pojo.CustomerExample;
import com.zero.logistics.service.BaseDataService;
import com.zero.logistics.service.CustomerService;
import com.zero.logistics.service.UserService;

/*
 *	@date:2020年3月31日 上午5:00:13
 *	@author:Zero
 *	@version:Future
 *	@description: 
 *
 */
@Controller
@RequestMapping("/baseData")
public class BaseDataController {
	
	@Autowired
	private BaseDataService baseDataService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private CustomerService customerService;

	@RequiresPermissions("baseData:baseDataPage")
	@RequestMapping("/baseDataPage")
	public String baseDataPage() {
		return "baseDataPage";
	}
	
	/**
	 * @function 分页显示基础数据
	 * @param pageNum
	 * @param pageSize
	 * @param keywords
	 * @return
	 */
	@RequiresPermissions("baseData:list")
	@RequestMapping("list")
	@ResponseBody  //返回json类型的数据
	public PageInfo<BaseData> baseDataList(@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "10")int pageSize,
			String keywords){
		
		PageHelper.startPage(pageNum, pageSize);
		BaseDataExample example = new BaseDataExample();
		
		// 定义搜索的逻辑（使用管理）
		if (StringUtils.isNotBlank(keywords)) {  //当前台传过来的关键字不等于空的时候
			//根据基础数据名查询
			Criteria criteria1 = example.createCriteria();
			criteria1.andBaseNameLike("%" + keywords + "%");
			
			//根据父基础数据名查询
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%" + keywords + "%");
			example.or(criteria2);
		}
		
		List<BaseData> baseDatas = baseDataService.selectByExample(example);
		PageInfo<BaseData> pageInfo = new PageInfo<>(baseDatas);
		
		return pageInfo;
	}
	
	/**
	 * @function 删除基础数据
	 * @param baseId
	 * @return
	 */
	@RequiresPermissions("baseData:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject baseDataDelete(Long baseId) {
		//1.删除前判断基础数据下是否有子基础数据，如果有则不能删除
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(baseId);
		List<BaseData> childBaseDatas = baseDataService.selectByExample(example);
		if (childBaseDatas.size() != 0) {
			return MessageObject.createMo(0, "该基础数据还关联有子基础数据，不能删除");
		}
		
		//2.删除之前判断被删除的基础数据是否为区间，如果为区间则需要判断该区间是否还有关联的客户，如果有则不能删除
		BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
		if (baseData.getParentId() == 1) {  //parentId为1的是区间的基础数据
			CustomerExample example2 = new CustomerExample();
			com.zero.logistics.pojo.CustomerExample.Criteria criteria2 = example2.createCriteria();
			criteria2.andBaseIdEqualTo(baseId);
			List<Customer> customers = customerService.selectByExample(example2);
			if (customers.size() > 0) {
				return MessageObject.createMo(0, "该基础数据还关联有客户数据，不能删除");
			}
			
		}
		
		//3.根据基础数据id删除基础数据
		int row = baseDataService.deleteByPrimaryKey(baseId);
		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 批量删除(以String类型接收前端传过来的参数)
	 * @param map
	 * @return
	 */
	@RequiresPermissions("baseData:batchdel")
	@RequestMapping("/batchdel")
	@ResponseBody
	public MessageObject baseDataBatchdel(String baseIds) {
		System.out.println("需要批量删除的baseIds: " + baseIds);
		String[] baseIdsArr = baseIds.split(",");
		
		//1.删除前判断基础数据下是否有子基础数据，如果有则不能删除
		for (String baseId : baseIdsArr) {
			BaseDataExample example = new BaseDataExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(Long.parseLong(baseId));
			List<BaseData> childBaseDatas = baseDataService.selectByExample(example);
			if (childBaseDatas.size() != 0) {
				return MessageObject.createMo(0, "将被删除的基础数据下还关联有子基础数据，不能删除");
			}
			
			//2.删除之前判断被删除的基础数据是否为区间，如果为区间则需要判断该区间是否还有关联的客户，如果有则不能删除
			BaseData baseData = baseDataService.selectByPrimaryKey(Long.parseLong(baseId));
			if (baseData.getParentId() == 1) {  //parentId为1的是区间的基础数据
				CustomerExample example2 = new CustomerExample();
				com.zero.logistics.pojo.CustomerExample.Criteria criteria2 = example2.createCriteria();
				criteria2.andBaseIdEqualTo(Long.parseLong(baseId));
				List<Customer> customers = customerService.selectByExample(example2);
				if (customers.size() > 0) {
					return MessageObject.createMo(0, "被删除的基础数据还关联有客户数据，不能删除");
				}
			}
		}
		
		//3.根据基础数据id的数组批量删除基础数据
		int rows = baseDataService.deleteByIds(baseIdsArr);
		MessageObject mo = MessageObject.createMo(0, "批量删除数据失败，请联系技术人员");
		if (rows != 0) {
			mo = MessageObject.createMo(1, "批量删除数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 提供编辑页面(在新增基础数据和更新基础数据的时候都需要执行此方法)
	 * @param m
	 * @return
	 */
	@RequestMapping("/edit")
	public String baseDataEdit(Model m, Long baseId) {	
		//1.当前端传过来的baseId不为空的时候，就通过baseId查询基础数据信息并共享数据，用于更新的情况
		if (baseId != null) {
			BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
			System.out.println("需要更新的基础数据信息：" + baseData.getBaseId());
			m.addAttribute("baseData", baseData);
		}
		
		//2.获取所有的父基础名称信息并共享（查询出所有parentId为空的基础数据作为父基础数据）
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdIsNull();
		List<BaseData> parentBaseDatas = baseDataService.selectByExample(example);
		m.addAttribute("parentBaseDatas", parentBaseDatas);
		
		return "baseDataEdit";
	}
	
	/**
	 * @function 判断基础数据名是否存在
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("baseData:checkBaseName")
	@RequestMapping("/checkBaseName")
	public boolean baseDataCheckName(String baseName) {
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andBaseNameEqualTo(baseName);
		List<BaseData> baseDatas = baseDataService.selectByExample(example);
		return baseDatas.size() == 0 ? true : false;
	}
	
	/**
	 * @function 新增基础数据
	 * @param baseData
	 * @return
	 */
	@RequiresPermissions("baseData:insert")
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject baseDataInsert(BaseData baseData) {
		// 1.插入数据并返回结果
		int row = baseDataService.insert(baseData);
		MessageObject mo = MessageObject.createMo(0, "添加数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "添加数据成功");
		}
		return mo;
	}
	
	/**
	 * @function 更新基础数据信息
	 * @param baseData
	 * @return
	 */
	@RequiresPermissions("baseData:update")
	@RequestMapping("update")
	@ResponseBody
	public MessageObject baseDataUpdate(BaseData baseData) {
		// 1.更新数据并返回结果(这里要用动态sql语句)
		int row = baseDataService.updateByPrimaryKeySelective(baseData);
		MessageObject mo = MessageObject.createMo(0, "更新数据失败，请联系技术人员");
		if (row == 1) {
			mo = MessageObject.createMo(1, "更新数据成功");
		}
		return mo;
	}

}
